package au.com.handsunited.web.validator.user;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import au.com.handsunited.web.command.user.UserSearchCommand;

/**
 * 
 * @author arunda
 */
public class UserSearchCommandValidator implements Validator {

	
	private static Logger logger = LoggerFactory.getLogger(UserSearchCommandValidator.class);

	
	/**
	 * 
	 */
	public UserSearchCommandValidator() {
		super();
	}

	/**
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	@Override
	public boolean supports(Class clazz) {
		return clazz.isAssignableFrom(UserSearchCommand.class);
	}

	/**
	 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
	 */
	@Override
	public void validate(Object target, Errors errors) {

		UserSearchCommand userSearchCommand = (UserSearchCommand) target;
		
		if(StringUtils.isBlank(userSearchCommand.getFirstName()) && 
				StringUtils.isBlank(userSearchCommand.getLastName()) && 
				StringUtils.isBlank(userSearchCommand.getEmailAddress())) {
			errors.reject("", "At least one search field is required.");
		}
		
	}

}
